<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The DateTimeInterface interface</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="datetimeimmutable.sub.html">« DateTimeImmutable::sub</a></li>
      <li style="float: right;"><a href="datetime.diff.html">DateTimeInterface::diff »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.datetime.html">日期/时间</a></li>
    <li>The DateTimeInterface interface</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.datetimeinterface" class="reference">

 <h1 class="title">The DateTimeInterface interface</h1>
 

 <div class="partintro"><p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p>


  <div class="section" id="datetimeinterface.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    创建 <span class="interfacename"><strong class="interfacename">DateTimeInterface</strong></span> 是为了让参数、返回值、属性类型声明能够接受
    <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span> 或 <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span>
    作为值。不可以使用用户定义的类实现该接口。
   </p>
   <p class="para">
    此接口也定义了公共常量，允许通过 <span class="methodname"><a href="datetime.format.html" class="methodname">DateTimeImmutable::format()</a></span>
    和 <span class="methodname"><a href="datetime.format.html" class="methodname">DateTime::format()</a></span> 格式化 <span class="classname"><a href="class.datetimeimmutable.html" class="classname">DateTimeImmutable</a></span>
    或 <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> 对象。
   </p>
  </div>


  <div class="section" id="datetimeinterface.synopsis">
   <h2 class="title">接口摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
     
    </div>

    <div class="classsynopsisinfo">
     <span class="oointerface">
      <span class="modifier">interface</span> <strong class="interfacename">DateTimeInterface</strong></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 常量 */</div>
    <div class="fieldsynopsis">
     <span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.atom"><var class="varname">ATOM</var></a></var><span class="initializer"> = &quot;Y-m-d\\TH:i:sP&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.cookie"><var class="varname">COOKIE</var></a></var><span class="initializer"> = &quot;l, d-M-Y H:i:s T&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.iso8601"><var class="varname">ISO8601</var></a></var><span class="initializer"> = &quot;Y-m-d\\TH:i:sO&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.iso8601-expanded"><var class="varname">ISO8601_EXPANDED</var></a></var><span class="initializer"> = &quot;X-m-d\\TH:i:sP&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc822"><var class="varname">RFC822</var></a></var><span class="initializer"> = &quot;D, d M y H:i:s O&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc850"><var class="varname">RFC850</var></a></var><span class="initializer"> = &quot;l, d-M-y H:i:s T&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc1036"><var class="varname">RFC1036</var></a></var><span class="initializer"> = &quot;D, d M y H:i:s O&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc1123"><var class="varname">RFC1123</var></a></var><span class="initializer"> = &quot;D, d M Y H:i:s O&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc7231"><var class="varname">RFC7231</var></a></var><span class="initializer"> = &quot;D, d M Y H:i:s \\G\\M\\T&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc2822"><var class="varname">RFC2822</var></a></var><span class="initializer"> = &quot;D, d M Y H:i:s O&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc3339"><var class="varname">RFC3339</var></a></var><span class="initializer"> = &quot;Y-m-d\\TH:i:sP&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rfc3339-extended"><var class="varname">RFC3339_EXTENDED</var></a></var><span class="initializer"> = &quot;Y-m-d\\TH:i:s.vP&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.rss"><var class="varname">RSS</var></a></var><span class="initializer"> = &quot;D, d M Y H:i:s O&quot;</span>;</div>

    <div class="fieldsynopsis"><span class="modifier">public</span>
     <span class="modifier">const</span>
     <span class="type">string</span>
      <var class="fieldsynopsis_varname"><a href="class.datetimeinterface.html#datetimeinterface.constants.w3c"><var class="varname">W3C</var></a></var><span class="initializer"> = &quot;Y-m-d\\TH:i:sP&quot;</span>;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="datetime.diff.html" class="methodname">diff</a></span>(<span class="methodparam"><span class="type"><a href="class.datetimeinterface.html" class="type DateTimeInterface">DateTimeInterface</a></span> <code class="parameter">$targetObject</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$absolute</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><a href="class.dateinterval.html" class="type DateInterval">DateInterval</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="datetime.format.html" class="methodname">format</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="datetime.getoffset.html" class="methodname">getOffset</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="datetime.gettimestamp.html" class="methodname">getTimestamp</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="datetime.gettimezone.html" class="methodname">getTimezone</a></span>(): <span class="type"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span>|<span class="type"><span class="type false">false</span></span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="datetime.wakeup.html" class="methodname">__wakeup</a></span>(): <span class="type"><span class="type void">void</span></span></div>

   }</div>


  </div>


  <div class="section" id="datetimeinterface.constants.types">
   <h2 class="title">预定义常量</h2>
   <dl>
    
     <dt id="datetimeinterface.constants.atom"><strong><code>DateTimeInterface::ATOM</code></strong></dt>

     <dt>
<strong><code>DATE_ATOM</code></strong></dt>

     <dd>

      <span class="simpara">
       Atom 格式（示例：2005-08-15T15:52:01+00:00）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.cookie"><strong><code>DateTimeInterface::COOKIE</code></strong></dt>

     <dt>
<strong><code>DATE_COOKIE</code></strong></dt>

     <dd>

      <span class="simpara">
       HTTP Cookies 格式（示例：Monday, 15-Aug-2005 15:52:01 UTC）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.iso8601"><strong><code>DateTimeInterface::ISO8601</code></strong></dt>

     <dt>
<strong><code>DATE_ISO8601</code></strong></dt>

     <dd>

      <span class="simpara">
       ISO-8601 格式（示例：2005-08-15T15:52:01+0000）
      </span>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        这种格式和 ISO-8601 格式并不兼容，只是出于向后兼容的原因才保留的。
        如果要使用和 ISO-8601 兼容的格式，请使用
        <strong><code>DateTimeInterface::ISO8601_EXPANDED</code></strong>
        和 <strong><code>DateTimeInterface::ATOM</code></strong> 两个常量。
       </span>
      </p></blockquote>
     </dd>

    

    
     <dt id="datetimeinterface.constants.iso8601-expanded"><strong><code>DateTimeInterface::ISO8601_EXPANDED</code></strong></dt>

     <dt>
<strong><code>DATE_ISO8601_EXPANDED</code></strong></dt>

     <dd>

      <span class="simpara">
       ISO-8601 扩大版（示例：+10191-07-26T08:59:52+01:00）
      </span>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        通过始终包含符号字符，此格式允许超出 ISO-8601
        正常范围（<code class="literal">0000</code>-<code class="literal">9999</code>）的年份。还解决了时区部分（<code class="literal">+01:00</code>）与
        ISO-8601 兼容的问题。
       </span>
      </p></blockquote>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rfc822"><strong><code>DateTimeInterface::RFC822</code></strong></dt>

     <dt>
<strong><code>DATE_RFC822</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 822 格式（示例：Mon, 15 Aug 05 15:52:01 +0000）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rfc850"><strong><code>DateTimeInterface::RFC850</code></strong></dt>

     <dt>
<strong><code>DATE_RFC850</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 850 格式（示例：Monday, 15-Aug-05 15:52:01 UTC）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rfc1036"><strong><code>DateTimeInterface::RFC1036</code></strong></dt>

     <dt>
<strong><code>DATE_RFC1036</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 1036（示例：Mon, 15 Aug 05 15:52:01 +0000）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rfc1123"><strong><code>DateTimeInterface::RFC1123</code></strong></dt>

     <dt>
<strong><code>DATE_RFC1123</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 1123 格式（示例：Mon, 15 Aug 2005 15:52:01 +0000）
      </span>
     </dd>

    

    
      <dt id="datetimeinterface.constants.rfc7231"><strong><code>DateTimeInterface::RFC7231</code></strong></dt>

      <dt>
<strong><code>DATE_RFC7231</code></strong></dt>

      <dd>

       <span class="simpara">
        RFC 7231 格式 (自 PHP 7.0.19 和 7.1.5 可用) (示例：Sat, 30 Apr 2016 17:52:13 GMT)
       </span>
      </dd>

    

    
     <dt id="datetimeinterface.constants.rfc2822"><strong><code>DateTimeInterface::RFC2822</code></strong></dt>

     <dt>
<strong><code>DATE_RFC2822</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 2822 格式（示例：Mon, 15 Aug 2005 15:52:01 +0000）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rfc3339"><strong><code>DateTimeInterface::RFC3339</code></strong></dt>

     <dt>
<strong><code>DATE_RFC3339</code></strong></dt>

     <dd>

      <span class="simpara">
       同 <strong><code>DATE_ATOM</code></strong>（自 PHP 5.1.3 版本可用）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rfc3339-extended"><strong><code>DateTimeInterface::RFC3339_EXTENDED</code></strong></dt>

     <dt>
<strong><code>DATE_RFC3339_EXTENDED</code></strong></dt>

     <dd>

      <span class="simpara">
       RFC 3339 EXTENDED 格式（示例：2005-08-15T15:52:01.000+00:00）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.rss"><strong><code>DateTimeInterface::RSS</code></strong></dt>

     <dt>
<strong><code>DATE_RSS</code></strong></dt>

     <dd>

      <span class="simpara">
       RSS（示例：Mon, 15 Aug 2005 15:52:01 +0000）
      </span>
     </dd>

    

    
     <dt id="datetimeinterface.constants.w3c"><strong><code>DateTimeInterface::W3C</code></strong></dt>

     <dt>
<strong><code>DATE_W3C</code></strong></dt>

     <dd>

      <span class="simpara">
       RSS 格式（示例：2005-08-15T15:52:01+00:00）
      </span>
     </dd>

    

   </dl>

  </div>


 <div class="section" id="datetimeinterface.changelog">
  <h2 class="title">更新日志</h2>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.2.0</td>
       <td>
        新增常量 <strong><code>DateTimeInterface::ISO8601_EXPANDED</code></strong>。
       </td>
      </tr>
     
      <tr>
       <td>7.2.0</td>
       <td>
       <span class="classname"><a href="class.datetime.html" class="classname">DateTime</a></span> 的类常量现在定义在了 <span class="classname"><strong class="classname">DateTimeInterface</strong></span> 上。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 </div>

 



































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="datetime.diff.html">DateTimeInterface::diff</a> — 返回两个 DateTime 之间的差值</li><li><a href="datetime.format.html">DateTimeInterface::format</a> — 按照指定格式返回格式化后的日期</li><li><a href="datetime.getoffset.html">DateTimeInterface::getOffset</a> — Returns the timezone offset</li><li><a href="datetime.gettimestamp.html">DateTimeInterface::getTimestamp</a> — Gets the Unix timestamp</li><li><a href="datetime.gettimezone.html">DateTimeInterface::getTimezone</a> — Return time zone relative to given DateTime</li><li><a href="datetime.wakeup.html">DateTime::__wakeup</a> — The __wakeup handler</li></ul>
</div>
</div></div></body></html>